package io.bluefw.blueui.dsl.render;

import java.nio.charset.Charset;

import com.google.common.base.Optional;

public abstract class Render {
    private String templateName;
    private final Charset charset;
    protected final FreemarkerTemplate freemarker;
    
    protected Render(String templateName) {
        this(templateName, null);
    }
    
    protected Render(String templateName, Charset charset) {
        this.templateName = resolveName(templateName);
        this.charset = charset;
        this.freemarker = FreemarkerTemplate.getInstance();
    }
    
    public String getTemplateName() {
        return templateName;
    }
    
    protected void setTemplateName(String templateName) {
    	this.templateName = resolveName(templateName);
    }
    
    public Optional<Charset> getCharset() {
        return Optional.fromNullable(charset);
    }
    
    private String resolveName(String templateName) {
        if (templateName.startsWith("/")) {
            return templateName;
        }
        final String packagePath = getClass().getPackage().getName().replace('.', '/');
        return String.format("/%s/%s", packagePath, templateName);
    }
    
    public abstract StringBuffer render();
}
